﻿using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Version3.SharePoint;

//if you want to be able to display data in a SharePoint form
//as well as access it programatically, then you have to be
//able to rehydrate your own object. Its really pretty easy
//you simply inherit this object and override the render function
//and they store your objects class definition in the
//column configuration for the field. Ok not so simple but it 
//is the "second" time, your type should be defined in the same
//assembly that you put your renderer in.

//assuming that this assembly is in the GAC as SampleRenderer.DLL with the signing key provided 
// the configuration render string for it would be
//SampleRenderer.SampleFieldRenderer,SampleRenderer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=507b8f13bb7d1147

namespace SampleRenderer
{

    //lets define some arbitrary structures
    //that we may want to store for some reason
    //If you dont need to be able to render them on a form
    //they dont need to be publically accessible outside of your
    //application. If you do, then we need to be able to load
    //them whenever they are rehydrated.

    [Serializable]
    public struct FileBytesInColumn
    {
        public string Name;
        public string OriginalPath;
        public byte[] Bytes;
    }

    [Serializable]
    public struct TestStruct
    {
        public Guid Id;
        public DateTime When;

    }
    [Serializable]
    public struct DifferentTestStruct
    {
        public string Name;
        public TestStruct[] ArrayOfTestStruct;
    }

    //the remaining part of this is a sample on how you can 
    //draw on the SharePoint standard edit form if your assembly
    //has a registered renderer, the registration is actually
    //in the text of the column
    public class SampleFieldRenderer : FreezeDriedDataFieldRenderer
    {

        public override void Render(Panel formPanel)
        {
            Table dataTable = new Table();
            formPanel.Controls.Add(dataTable);
            dataTable.CellPadding = 0;
            dataTable.CellSpacing = 0;
            dataTable.BorderWidth = 0;
            if (base.RehydratedObject.GetType() == typeof(FileBytesInColumn))
            {
                TableRow dataRow = new TableRow();
                dataTable.Controls.Add(dataRow);
                TableCell dataCell = new TableCell();
                dataRow.Controls.Add(dataCell);
                dataCell.Text = ((FileBytesInColumn)base.RehydratedObject).Name;
                dataRow = new TableRow();
                dataTable.Controls.Add(dataRow);
                dataCell = new TableCell();
                dataRow.Controls.Add(dataCell);
                dataCell.Text = "Bytes = " + ((FileBytesInColumn)base.RehydratedObject).Bytes.Length.ToString(); ;
            }
            if (base.RehydratedObject.GetType() == typeof(TestStruct))
            {
                TableRow dataRow = new TableRow();
                dataTable.Controls.Add(dataRow);
                TableCell dataCell = new TableCell();
                dataRow.Controls.Add(dataCell);
                dataCell.Text = ((TestStruct)base.RehydratedObject).Id.ToString("B");
                dataRow = new TableRow();
                dataTable.Controls.Add(dataRow);
                dataCell = new TableCell();
                dataRow.Controls.Add(dataCell);
                dataCell.Text = ((TestStruct)base.RehydratedObject).When.ToString();

            }
            if (base.RehydratedObject.GetType() == typeof(DifferentTestStruct))
            {
                TableRow dataRow = new TableRow();
                dataTable.Controls.Add(dataRow);
                TableCell dataCell = new TableCell();
                dataRow.Controls.Add(dataCell);
                dataCell.Text = ((DifferentTestStruct)base.RehydratedObject).Name;
                TestStruct[] testArray = ((DifferentTestStruct)base.RehydratedObject).ArrayOfTestStruct;
                for (int arrayIndex = 0; arrayIndex < testArray.Length; arrayIndex++)
                {
                    dataRow = new TableRow();
                    dataTable.Controls.Add(dataRow);
                    dataCell = new TableCell();
                    dataRow.Controls.Add(dataCell);
                    dataCell.Text = testArray[arrayIndex].Id.ToString("B");
                    dataRow = new TableRow();
                    dataTable.Controls.Add(dataRow);
                    dataCell = new TableCell();
                    dataRow.Controls.Add(dataCell);
                    dataCell.Text = testArray[arrayIndex].When.ToString();
                }
            }

        }
    }


}
